//
//  StartViewController.m
//  RootDirectory
//
//  Created by Ryan on 13-2-28.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "StartViewController.h"

@interface StartViewController ()

@property (nonatomic, retain) NSArray *guideArray;

@end

@implementation StartViewController

@synthesize startImageView;
@synthesize startScrollView;
@synthesize guideArray;

- (void)showPannel
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kShowPannelViewNotification object:nil];
}
- (void)showGuide
{
    [self.view addAnimationWithType:kCATransitionFade subtype:nil];
    self.startImageView.hidden = YES;
}
#pragma mark - Gesture methods
- (void)swipeWithGesture:(UISwipeGestureRecognizer *)gesture
{
    if(self.startScrollView.contentOffset.x >= (self.guideArray.count-1)*self.startScrollView.frame.size.width)
    {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:kIsWelcomeShown];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:kShowPannelViewNotification object:@"fromGuide"];
    }
}

#pragma mark - UIViewController methods
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if(IsDevicePhone5)
    {
        self.startImageView.image = [UIImage imageNamed:@"Default-568h@2x.png"];
        self.guideArray = [NSArray arrayWithObjects:@"welcome1_568@2x.jpg",@"welcome2_568@2x.jpg",@"welcome3_568@2x.jpg",@"welcome4_568@2x.jpg",@"welcome5_568@2x.jpg", nil];
    }
    else
    {
        self.startImageView.image = [UIImage imageNamed:@"Default.png"];
        self.guideArray = [NSArray arrayWithObjects:@"welcome1@2x.jpg",@"welcome2@2x.jpg",@"welcome3@2x.jpg",@"welcome4@2x.jpg",@"welcome5@2x.jpg", nil];
    }
    
    if([[[NSUserDefaults standardUserDefaults] objectForKey:kIsWelcomeShown] isEqualToString:@"1"])
    {
        self.startScrollView.hidden = YES;
        [self performSelector:@selector(showPannel) withObject:nil afterDelay:1.f];
    }
    else
    {
        self.startScrollView.hidden = NO;
        [self performSelector:@selector(showGuide) withObject:nil afterDelay:1.f];
        NSInteger index = 0;
        for(NSString *imgName in self.guideArray)
        {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(index*self.startScrollView.frame.size.width, 0, self.startScrollView.frame.size.width, self.startScrollView.frame.size.height)];
            imgView.image = [UIImage imageNamed:imgName];
            imgView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
            [self.startScrollView addSubview:imgView];
            [imgView release];
            index++;
        }
        [self.startScrollView setContentSize:CGSizeMake(self.startScrollView.frame.size.width*self.guideArray.count, [UIScreen mainScreen].bounds.size.height)];
        
        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeWithGesture:)];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
        swipeGesture.delegate = self;
        [self.view addGestureRecognizer:swipeGesture];
        [swipeGesture release];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        self.view = nil;
        self.startImageView = nil;
        self.startScrollView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.startImageView = nil;
    self.startScrollView = nil;
}

- (void)dealloc
{
    [guideArray release];
    [startScrollView release];
    [startImageView release];
    [super dealloc];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
@end
